package com.zjt.web;

import com.zjt.entity.VersionUpdate;
import com.zjt.service.IVersionUpdateService;
import com.zjt.web.api.vo.base.StateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;
/**
 * Created with IntelliJ IDEA.
 * User: xueyanbeijing
 * Date: 12-9-14
 * Time: PM2:48
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value="/web/version")
public class VersionUpdateController {
    private IVersionUpdateService versionUpdateService;
    private static String ios = "iphone";
    private static String andorid = "andorid";
    private static String windowsphone = "windows";
    private static String linux = "linux";

    @Autowired
    public void setVersionUpdateService(IVersionUpdateService versionUpdateService) {
        this.versionUpdateService = versionUpdateService;
    }

    @RequestMapping(value="/iosClientlist",method = RequestMethod.GET)
    public String iosClientlist(Model model)
    {
        List<VersionUpdate> list = this.versionUpdateService.getVersionUpdateListByVersionPlatform(ios);
        model.addAttribute("list", list);
        return "versionupdate/list";
    }
    @RequestMapping(value="/andoridlist",method = RequestMethod.GET)
    public String andoridlist(Model model)
    {
        List<VersionUpdate> list = this.versionUpdateService.getVersionUpdateListByVersionPlatform(andorid);
        model.addAttribute("list",list);
        return "versionupdate/list";
    }
    @RequestMapping(value="/terminallist",method = RequestMethod.GET)
    public String terminallist(Model model)
    {
        List<String> list = new ArrayList<String>();
        list.add("iOS4.3x +");
        list.add("andorid2.x +");
        return "versionupdate/terminallist";
    }

    @RequestMapping(value="/versionSynhroList",method = RequestMethod.GET)
    public String versionSynhroList(Model model)
    {
        List<VersionUpdate> list = this.versionUpdateService.getAllVersionUpdateList();
        model.addAttribute("list",list);
        return "versionupdate/versionSynhroList";
    }

    @RequestMapping(value="/versionSynhro/${id}",method = RequestMethod.GET)
    @ResponseBody
    public String versionSynhro(@PathVariable("id") Long id, RedirectAttributes redirectAttributes)
    {
        VersionUpdate versionUpdate = this.versionUpdateService.getVersionUpdateById(id);
        StateVO  stateVO = null;
        if (versionUpdate!=null)
        {
           stateVO = this.versionUpdateService.deplyVersion(versionUpdate.getVersionPlatform(),
                    versionUpdate.getVersionValue(),versionUpdate.getVersionString());
           if (stateVO.getCode()>=0)
           redirectAttributes.addFlashAttribute("message", versionUpdate.getVersionPlatform() + versionUpdate.getVersionString()+" 版本发布成功！");
        }
        return "redirect:/web/version/versionSynhroList";
    }
}
